package alipay.openplatform.edu.campus.jiuye.api.controller.topic;

import alipay.openplatform.edu.campus.jiuye.api.utils.ContextUtils;
import alipay.openplatform.edu.campus.jiuye.common.enums.ResultCode;
import alipay.openplatform.edu.campus.jiuye.model.vo.TopicInfoVO;
import alipay.openplatform.edu.campus.jiuye.model.vo.UserBaseInfo;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.topic.ITopicDetailInfoService;
import alipay.openplatform.edu.campus.jiuye.service.iservice.user.IUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;

@Controller
public class TopicDetailInfoController {
    private final static Logger log = LoggerFactory.getLogger(TopicDetailInfoController.class);
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ITopicDetailInfoService topicDetailInfoService;
    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 查看主题详情
     * url:/jiuye/api/getTopicInfoDetail
     *
     * @param topicId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/jiuye/api/getTopicInfoDetail", method = RequestMethod.POST)
    public ApiResult getTopicInfoDetail(@RequestParam(value = "topicId",required = true) Integer topicId) {
        log.info("查看主题后台详细信息[TopicDetailInfoController][getTopicInfoDetail]param{topicId:" + topicId + "}");
        TopicInfoVO topicInfoVO;
        try {
            topicInfoVO = topicDetailInfoService.selectDetailInfoByTopicID(topicId);
        } catch (Exception e) {
            log.error("查看主题后台详情异常", e);
            return ApiResult.build(ResultCode.HANDLE_FAIL.getCode(), ResultCode.HANDLE_FAIL.getName());
        }
        return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), topicInfoVO);
    }

    /***
     * 查看主题详情
     * @param topicId  id
     * @return
     */
//    @ResponseBody
//    @RequestMapping(value = "/jiuye/api/getTopicInfoDetailFront", method = RequestMethod.GET)
//    public ApiResult getTopicInfoDetailFront(@RequestParam(value = "topicId",required = true) Integer topicId,
//                                             @RequestParam(value = "cityId",required = false,defaultValue = "0")String cityId,
//                                             @RequestParam(value = "schoolId",required = false,defaultValue = "0")String schoolId,
//                                             @RequestParam(value = "schoolName",required = false,defaultValue = "未知")String schoolName,
//                                             @RequestParam(value = "cityName",required = false,defaultValue = "未知")String cityName) {
//        log.info("查看主题前台详细信息[TopicDetailInfoController][getTopicInfoDetail]param{topicId:" + topicId + "cityId:" + cityId + "schoolId:" + schoolId + "schoolName:" + schoolName + "cityName:" + cityName + "}");
//        UserBaseInfo userBaseInfo = getUserInfo();
//        if (userBaseInfo == null) {
//            return ApiResult.buildSuccess(ResultCode.AUTH_UNAUTHORIZED.getCode(), ResultCode.AUTH_UNAUTHORIZED.getName(), null);
//        }
//        TopicInfoVO topicInfoVO;
//        try {
//            topicInfoVO = topicDetailInfoService.selectDetailInfoByTopicID(userBaseInfo.getUserId(), cityId,schoolId,topicId,schoolName,cityName);
//        } catch (Exception e) {
//            log.error("查看主题前台详情异常", e);
//            return ApiResult.build(ResultCode.HANDLE_FAIL.getCode(), ResultCode.HANDLE_FAIL.getName());
//        }
//        return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), topicInfoVO);
//    }

    /**
     * 获取用户信息
     *
     * @return
     */
    private UserBaseInfo getUserInfo() {
        String userId = ContextUtils.getUserId(request);
 //       String userId = "2088302167243547";
        UserBaseInfo userBaseInfo = null;
        try {
            userBaseInfo = iUserInfoService.getUserInfoByUserId(userId);
        } catch (InvocationTargetException e) {
            log.error("TopicInfoController|getTopicInfoList|params:e={}", e);
        } catch (IllegalAccessException e) {
            log.error("TopicInfoController|getTopicInfoList|params:e={}", e);
        }
        return userBaseInfo;
    }


    @ResponseBody
    @RequestMapping(value = "/jiuye/api/getTopicInfoDetailInfo", method = RequestMethod.GET)
        public ApiResult getTopicInfoDetailInfo(@RequestParam(value = "topicId",required = true) Integer topicId,
                                        @RequestParam(value = "cityId",required = false,defaultValue = "0")String cityId,
                                        @RequestParam(value = "schoolId",required = false,defaultValue = "0")String schoolId) {
        log.info("查看主题前台详细信息[TopicDetailInfoController][getTopicInfoDetail]param{topicId:" + topicId + "cityId:" + cityId + "schoolId:" + schoolId + "}");
        UserBaseInfo userBaseInfo = getUserInfo();
        if (userBaseInfo == null) {
            return ApiResult.buildSuccess(ResultCode.AUTH_UNAUTHORIZED.getCode(), ResultCode.AUTH_UNAUTHORIZED.getName(), null);
        }
        TopicInfoVO topicInfoVO;
        try {
            topicInfoVO = topicDetailInfoService.selectDetailInfoByTopicIdAndSchoolId(userBaseInfo.getUserId(), topicId ,schoolId,cityId);
        } catch (Exception e) {
            log.error("查看主题前台详情异常", e);
            return ApiResult.build(ResultCode.HANDLE_FAIL.getCode(), ResultCode.HANDLE_FAIL.getName());
        }
        return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), topicInfoVO);
    }


}